package com.ruoyi.web.controller.server;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.EDiscountInfoLog;
import com.ruoyi.system.service.ISysConfigService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.EDiscountLog;
import com.ruoyi.system.service.IEDiscountLogService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 贴现人完善任务记录Controller
 *
 * @author ruoyi
 * @date 2022-04-21
 */
@RestController
@RequestMapping("/server/discountLog")
public class EDiscountLogController extends BaseController
{
    @Autowired
    private IEDiscountLogService eDiscountLogService;
    @Autowired
    private ISysConfigService configService;
    /**
     * 查询贴现人完善任务记录列表
     */
    @PreAuthorize("@ss.hasPermi('server:discountLog:list')")
    @GetMapping("/list")
    public TableDataInfo list(EDiscountLog eDiscountLog)
    {
        startPage();
        List<EDiscountLog> list = eDiscountLogService.selectEDiscountLogList(eDiscountLog);
        return getDataTable(list);
    }

    /**
     * 导出贴现人完善任务记录列表
     */
    @PreAuthorize("@ss.hasPermi('server:discountLog:export')")
    @Log(title = "贴现人完善任务记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export/{discountId}")
    public void export(HttpServletResponse response,@PathVariable("discountId") Long discountId)
    {
        List<EDiscountInfoLog> list= eDiscountLogService.exportDiscountLog(discountId);
        ExcelUtil<EDiscountInfoLog> util = new ExcelUtil<EDiscountInfoLog>(EDiscountInfoLog.class);
        util.exportExcel(response, list, "贴现人完善数据");
    }

    @Log(title = "任务记录", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('server:discountLog:import')")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file) throws Exception
    {
        ExcelUtil<EDiscountInfoLog> util = new ExcelUtil<EDiscountInfoLog>(EDiscountInfoLog.class);
        List<EDiscountInfoLog> eDiscountInfoLogs = util.importExcel(file.getInputStream());
        String operName = getUsername();
        LoginUser loginUser=getLoginUser();
        if (StringUtils.isNull(eDiscountInfoLogs) || eDiscountInfoLogs.size() == 0)
        {
            throw new ServiceException("导入数据不能为空！");
        }
        boolean onOff = Boolean.parseBoolean(configService.selectConfigByKey("server.taskLog.import.switch"));
        if(onOff){
            Integer num = Integer.valueOf(configService.selectConfigByKey("server.taskLog.import.num"));
            if (eDiscountInfoLogs.size()>num)
            {
                throw new ServiceException("导入数据条数过多不能超过"+num+"！");
            }
        }
        eDiscountLogService.importEDiscountInfoLog(eDiscountInfoLogs, operName, loginUser);
        return AjaxResult.success("注意：此操作为异步处理,建议一定时间之后刷新查看任务是否完成。");
    }

}
